package com.guigu.erp.controller;

import com.guigu.erp.pojo.SystemMenus;
import com.guigu.erp.service.SystemMenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
//import java.util.function.DoubleToIntFunction;

/**
 * ClassName:SystemMenusController
 * Package:com.guigu.erp.controller
 * Description:
 *
 * @Date:2024/5/5 10:23
 * @Author:chenchong
 */
@CrossOrigin
@Controller
@RequestMapping("/systemMenus")
public class SystemMenusController {

    @Autowired
    private SystemMenusService systemMenusService;

    @RequestMapping("tree")
    @ResponseBody
    public List<Map<String, Object>> tree() {
        //查询出所有的一级菜单
        List<SystemMenus> menus = systemMenusService.selById();
        List<Map<String, Object>> list = new ArrayList<>();
        //遍历所有一级菜单
        if (menus.size() > 0) {
            for (SystemMenus menu : menus) {
                Map<String, Object> map = new LinkedHashMap<>();
                map.put("id",menu.getId());
                map.put("name", menu.getName());
                map.put("icon",menu.getImageUrl());
                //根据父id查询子菜单
                map.put("children", systemMenusService.getChildren(menu.getId()));
                list.add(map);
            }
        }
        System.out.println(list);
        return list;
    }
}
